import com.google.common.base.Splitter;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Created by chenxin34 on 2019/7/22 下午5:32.
 */
public class IPUtils {
    public static void main(String[] args) throws UnknownHostException {
        ip2HostTestNew();
    }

    private static void ip2HostTest() throws UnknownHostException {
        String ip = "10.32.140.225";
        InetAddress inetAddress = InetAddress.getLocalHost();
        System.out.println(Arrays.toString(inetAddress.getAddress()));
        System.out.println(inetAddress.getHostName());
        System.out.println(inetAddress.getHostAddress());


        byte[] ipByte = new byte[4];
        List<String> ipValue = Splitter.on(".").splitToList(ip);
        int index = 0;
        for (String node : ipValue) {
            ipByte[index++] = (byte)(Integer.parseInt(node) & 0xff);
        }
        InetAddress test = Inet4Address.getByAddress(ipByte);
        System.out.println(test.getHostName());
    }

    private static void ip2HostTestNew() throws UnknownHostException {
        String ip = "10.32.140.225";

        byte[] ipByte = new byte[4];
        List<String> ipValue = Splitter.on(".").splitToList(ip);
        int index = 0;
        for (String node : ipValue) {
            ipByte[index++] = (byte)(Integer.parseInt(node) & 0xff);
        }
        InetAddress test = InetAddress.getByAddress(ipByte);
        System.out.println(test.getHostName());
    }
}
